// pages/tabbar/mine/mine.js
import { CommonModel } from "../../../api/api-common/commonModel.js"
import { isBindUser } from "../../../utils/bindUser.js"
import Dialog from '@vant/weapp/dialog/dialog';
const commonModel = new CommonModel()
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBind: true, // 是否绑定  默认已绑定
    nickName: "", // 用户昵称
    myName: "",
    showNickName: false,
    showNotice: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.key = wx.eventBus.newKey()
    wx.eventBus.on({
      key: this.key,
      event: 'change',
      listener: (params) => {
        this.setData({
          nickName: params.username
        })
        wx.nextTick(() => {
          this.setData({
            nickName: params.username
          })
        })
      }
    })
    // this.setData({
    //   nickName: App.globalData.username
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      nickName: App.globalData.username,
      showNotice: App.globalData.openid && App.globalData.mpid == null
    })
  },

  // 跳转
  navigateTo(e) {
    // 获取用户名
    const username = App.globalData.username;
    if (username != "") {
      // 走正常跳转
      const url = e.currentTarget.dataset.query;
      wx.navigateTo({
        url: url,
      })
    }
    else {
      this.setData({
        isBind: false
      })
    }
  },
  // 授权登录回调
  bindUserBlock(e) {
    const bind_code = e.detail.bind_code;
    if (bind_code == 200) {
      // 授权绑定成功, 获取用户信息
      commonModel.getInfosApi().then(result => {
        // 绑定了用户名
        App.globalData.username = result.name == null || result.name == "" ? "微信授权用户" : result.name;
        App.globalData.openid = result.openid;
        App.globalData.mpid = result.mpid;
        App.globalData.isLogin = 1;
        this.setData({
          nickName: App.globalData.username,
          isBind: true,
          showNotice: App.globalData.openid && App.globalData.mpid == null
        })
        wx.eventBus.post({
          params: {
            isLogin: 1
          },
          event: 'change'
        })
      })
    }
  },
  closeNotice() {
    this.setData({
      showNotice: false
    })
  },
  gotoNotice() {
    wx.navigateTo({
      url: '/pages/myNotice/noticeCode/noticeCode',
    })

  },
  gotoLogin() {
    const username = App.globalData.username;
    if (username == "") {
      // 未登录
      this.setData({
        isBind: false
      })
    }
  },
  // 成长时光
  switchTabTo() {
    wx.switchTab({
      url: '/pages/tabbar/growup/growup',
    })
  },
  // 修改昵称
  updateNickName() {
    const username = App.globalData.username;
    if (username != "") {
      this.setData({
        showNickName: true,
        myName: this.data.nickName
      })
    }
    else {
      // 未登录
      this.setData({
        isBind: false
      })
    }
  },
  // 确认修改昵称
  confirmNickName() {
    commonModel.updateNickNameApi({ name: this.data.myName }).then(res => {
      App.globalData.username = this.data.myName;
      this.setData({
        myName: this.data.myName,
        nickName: this.data.myName,
        showNickName: false
      })
    })
  },
  closeNickName() {
    this.setData({
      showNickName: false
    })
  },
  // 解除绑定
  unbindUser() {
    Dialog.confirm({
      title: '解除绑定',
      message: '确认用户解除绑定？',
      confirmButtonColor: "#4bbdf9",
    })
      .then(() => {
        // on confirm
        commonModel.unbindApi().then(res => {
          App.globalData.token = "";
          App.globalData.username = "";
          App.globalData.isBind = false;
          App.globalData.isLogin = 2;
          this.setData({
            nickName: ""
          })
          wx.eventBus.post({
            params: {
              isLogin: 2
            },
            event: 'change'
          })
          // wx.switchTab({
          //   url: '/pages/tabbar/growup/growup'
          // })
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})